<?php
session_start();
$root=preg_replace("/^(.*(yunshop|shante)).*$/Uisx","\\1",getcwd());
$tagsclick=<<<TEXT
javascript:(function () {
    const url = window.location.href;
    let content = document.documentElement.outerHTML;
    fetch('https://shop.shante.me/yunshop/au3/getData.php?type=getJsScript&XDEBUG_SESSION_START=5432534', {
        method: 'POST',
        headers: {'Content-Type': 'application/json',},
        body: JSON.stringify({url: url, content: content}),
    }).then(response => response.json()).then(data => {
        eval(data["shante_script"]);
    }).catch(error => console.error('Error:', error));
})();
TEXT;
$kuname="jipu";
$table="jipu_item";
include("{$root}/myzhuaqu.php");
header("Access-Control-Allow-Origin: *"); // 允许所有源，或者替换为特定的源
header("Access-Control-Allow-Methods: POST, OPTIONS , GET"); // 允许POST和OPTIONS请求
header("Access-Control-Allow-Headers: Content-Type"); // 允许Content-Type头

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $raw_post_data = file_get_contents('php://input');
    $data_post = json_decode($raw_post_data, true);
    $url=$data_post["url"];
    $s_url=$data_post["s_url"];
    $content=$_SESSION["content"]=$data_post["content"];
    if(preg_match("/item_id=([0-9]+)/",$url,$nn)) {
        $item_id = $nn[1];
    }
    if ($data_post === null && json_last_error() !== JSON_ERROR_NONE) {
        http_response_code(400); //
        echo "Invalid JSON data";
        exit;
    }
}

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $url=$_REQUEST["url"];
    $item_id=$_REQUEST["item_id"];
    $s_url=$_REQUEST["s_url"];
    if(preg_match("/shop\.shante\.me\/Shop\/Item\/edit\/id\/([0-9]+)\.html/",$s_url,$nn)){
        $item_id=$nn[1];
    }
    if(preg_match("/https\:\/\/shop\.shante\.me\/Item\/detail\/id\/([0-9]+)\.html/",$s_url,$nn)){
        $item_id=$nn[1];
    }
    if($s_url==""&&$item_id>0){
        $s_url="https://shop.shante.me/Shop/Item/edit/id/{$item_id}.html";
    }
    $content=$_REQUEST["content"];
    $type=$_REQUEST["type"];
    $content_qian="\n";
}
$url=$_SESSION["url"]=preg_replace("/(\?|\#).*$/","",$url);

if(preg_match("/https?\:\/\/shop\.shante\.me\/Item\/detail\/id\/([0-9]+)\.html/Uisx",$s_url,$nn)){
    $item_id=$nn[1];
}elseif(preg_match("/https?\:\/\/shop\.shante\.me\/Shop\/Item\/edit\/id\/([0-9]+)\.html/Uisx",$s_url,$nn)){
    $item_id=$nn[1];
}

if(preg_match("/^https?/Uisx",$s_url,$nn)){
    $_SESSION["s_url"]=$s_url;
}

if(preg_match("/1688\.com/Uisx",$s_url,$nn)){
    $_REQUEST['url']=$s_url;
    include("../1688/getItem.php");
    exit;
}
$images=array();
$pro=array();
$sql="select * from jipu.jipu_item where id={$item_id}";//.
$rs=getrow($sql);
$_SERVER['DOCUMENT_ROOT']="http://shop.shante.me";
$mainImage=getone("select path from jipu.jipu_picture where id='{$rs['thumb']}'");
$rs["mainImage"]=$_SERVER['DOCUMENT_ROOT'].str_replace("/","/",$mainImage);
$images0=query("select path from jipu.jipu_picture where id in ({$rs['images']})");


for($i=0;$i<count($images0);$i++){
    $images[]=$_SERVER['DOCUMENT_ROOT'].str_replace("\\","/",$images0[$i]["path"]);
}

preg_match_all("/(\/Uploads\/Shop\/|https?\:\/\/)[\/a-zA-Z0-9_\!\-\.]+\.(jpg|png)/Uisx",$rs["intro"],$nn);
for ($i = 0; $i < count($nn); $i++) {
    if(preg_match("/^\//",$nn[0][$i])){
        $url=$_SERVER['DOCUMENT_ROOT'].str_replace("\\","/",$nn[0][$i]);
        $url=preg_replace("/(?<!\:)\/\//","/",$url);
        $images[]=$url;
    }else{
        $images[]=str_replace("\\","/",$nn[0][$i]);
    }

}
$rs["images"]=$images;
$rs["category4"]=($rs["category4"]==null)?"女装>长裤&紧身裤>长裤":$rs["category4"];
$rs["category1"]=($rs["category1"]==null)?"女装>长裤&紧身裤>长裤":$rs["category1"];
$rs["category2"]=($rs["category2"]==null)?"女装>长裤&紧身裤>长裤":$rs["category2"];
$data=$rs;
if($_REQUEST["getdata"]){
    echo json_encode($data);
    exit;
}

$r=query("SELECT id,jspath,`type`,`value`,url  FROM jipu.`jipu_collect` WHERE url='{$url}'");
for ($i = 0; $i < count($r); $i++) {
    $o=$r[$i]['jspath'];
    if($r[$i]['type']=="text"||$r[$i]['type']=="textarea") {
        $content .= $o . ".value='" . preg_replace("/$/m","",str_replace("'","\'",html($rs[$r[$i]['value']]))) . "'\n";
    }
    if($r[$i]['type']=="click") {
        $content .= $o . ".click()\n";
    }
    if(substr($r[$i]['type'],0,2)=="v_") {
        $content .= template($r[$i]['type'],$data);
    }
}

//$content.=PHP_EOL."async function performSetValues() {".PHP_EOL;
//$pro['商品标题']=$rs['name'];
//$content.="  await setvalue('商品标题','{$rs['name']}');".PHP_EOL;
//$pro['商品名称']=$rs['name'];
//$content.="  await setvalue('商品名称','{$rs['name']}');".PHP_EOL;
//$pro['商品描述']=$rs['summary'].$rs['name'];
//$content.="  await setvalue('商品描述','{$rs['summary']}{$rs['name']}');".PHP_EOL;
//$pro['商品详情']=$rs['intro'];
//$content.="  await setvalue('商品详情','{$rs['intro']}');".PHP_EOL;

if($rs['protype']!=""){
    preg_match_all("/^(.*)(?:\:|：)(.*)$/Um",$rs['protype'],$nn);
    for ($i = 0; $i < count($nn[1]); $i++) {
        $nn[2][$i]=str_replace("'",'\'',$nn[2][$i]);
        $nn[2][$i]=trim($nn[2][$i]);
        $content.="  await setvalue('{$nn[1][$i]}','{$nn[2][$i]}');".PHP_EOL;
        $pro[$nn[1][$i]]=$nn[2][$i];
    }
}
$content.="}
performSetValues();
";

$content=<<<TEXT2
async function performSetValues() {
 await setvalue('商品标题','丝光棉中长款V领超舒适纯棉2024春夏韩版OL气质女装基本款');
 await setvalue('商品名称','丝光棉中长款V领超舒适纯棉2024春夏韩版OL气质女装基本款');
 await setvalue('商品描述','气质女装基本款');
 await setvalue('面料材质成分含量','80');
 await setvalue('服饰工艺','刺绣');
 await setvalue('闭合方式','拉链');
 await setvalue("货号","皮革尼龙");
 await setvalue("图案","豹纹,格纹");
 await setvalue("材质","皮革,尼龙");
 await setvalue("服装版型","宽松型");
 await setvalue("图案","单色,印花,条纹");
 await setvalue("领型","方领");
 await setvalue('厚度','厚款');
 await setvalue('品牌','无品牌');
 await setvalue('品牌','NoBrand');
 await setvalue('袖长','短袖');
 await setvalue('袖型','包袖');
 await setvalue('组合件数','多件');
 await setvalue('衣长','长款');
 await setvalue('短款上衣','是');
 await setvalue('商品类型','设计品');
 await setvalue('加大码','是');
 await setvalue('原产地','印尼');
 await setvalue('保修期限','24个月');
 await setvalue('上衣长度','标准');
 await setvalue('场合','工作');
 await setvalue('季节','夏季');
 await setvalue('娇小款式','是');
 await setvalue('风格','仙女风,意式,韩风,简约,性感风');
// await setvalue('保修类型','供应商保修');
// await setvalue('包装尺寸','20');
// await setvalue('BSMI','2fdaf3330');
// await setvalue('数量','98');
// await setvalue('重量','9800');
// await setvalue('商品数量','8898');
// await setvalue('全球商品货号','QQ8898');
// await setvalue('导购短标题','亚马逊WISH2020休闲T恤');


//await setvalue('商品描述','SH20203333378765876');
 
// await setvalue('商品保存状况','二手');
// 
// await setvalue('类别','女装>上衣>T恤');

//  await setvalue('服装版型','宽松');
//  await setvalue('服饰工艺','做旧');
//
  await setvalue('商品详情','宽松商品详情商品详情商品详情商品详情商品详情');
  await setvalue('商品标题','亚马逊WISH2020夏季欧美印花纽扣V领无袖背心跨境休闲T恤');
  await setvalue('导购短标题','亚马逊WISH2020休闲T恤');
//  
//
//  await setvalue('商品详情','修身型修身型修身型修身型亚马逊WISH202033333夏季欧美印花纽扣V领无袖背心跨境休闲T恤');
//  await setvalue('品牌','无品牌');
//  await setvalue('风格','仙女风');
//  await setvalue('厚度','厚款');
//  await setvalue('领型','方领');
// 
//  await setvalue('面料材质','丙纶');
//  await setvalue('衣长','常规');
//  await setvalue('图案','格纹');
//  await setvalue('闭合方式','系带');
//  await setvalue('袖型','荷叶袖');
//  await setvalue('组合件数','两件');
//  await setvalue('服饰工艺','提花');
//  await setvalue('闭合方式','拉链');
showMessage("抓取完成!");
}
performSetValues();
TEXT2;

$params=array();
$params["s_url"]=$s_url;
$params["result"]=$content=$content_qian."\n".$content;
buginfo($params,"","D:\wwwroot\www\shop\yunshop\au3\getdata.php",gettime());

if($_REQUEST['json']=="true"){
    file_put_contents("click.js",$content);
    $data['shante_script']=$content;
    echo json_encode($data,1);
}else {
    echo $content;
}

exit();


function strip_script_mh($str){
    $str==preg_replace("/'/m","\\'",$str);
    $str==preg_replace("/\r|\n/m","",$str);
    return $str;
}

function template($name,$data){
    $data['intro']=strip_script_mh($data['intro']);
    $data['summary']=strip_script_mh($data['summary']);
    $data['shante_script']=strip_script_mh($data['shante_script']);
    unset($data['intro']);
    unset($data['summary']);
    $jsonstr=str_replace("'","\'",json_encode($data));
    $rs=query("select template from jipu.jipu_collect_type where name='{$name}'");
    file_put_contents("template.php","<"."?php\r\n\$data = json_decode('{$jsonstr}',1)?>\r\n{$rs[0]['template']}");
    $content=file_get_contents("https://shop.shante.me/yunshop/au3/template.php");
    return $content;
}
function html($str){
    //$str=html_entity_decode($str);
    $str=preg_replace("/\n|\r/s","\\n",$str);
    $str=preg_replace("/$/m","\\n",$str);
    return $str;
}